package com.example.controller;

import com.example.entity.Vehicle;
import com.example.entity.VehicleStatus;
import com.example.entity.VehicleType;
import com.example.repository.VehicleRepository;
import com.example.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/vehicles")
public class VehicleController {

    @Autowired
    private VehicleRepository vehicleRepository;
    
    @Autowired
    private VehicleService vehicleService;

    @PostMapping("/autonomous")
    public String addAutonomousVehicle(@RequestBody Map<String, Object> request) {
        String licensePlate = request.get("licensePlate").toString();
        String model = request.get("model").toString();
        String typeStr = "IS_AUTONOMOUS";
        VehicleType type = VehicleType.valueOf(typeStr);
        int seats = Integer.parseInt(request.get("seats").toString());
        boolean canCarpool = Boolean.parseBoolean(request.get("canCarpool").toString());

        // 检查车牌是否重复
        if (vehicleRepository.existsById(licensePlate)) {
            return "该车牌已被使用！";
        }

        Vehicle vehicle = new Vehicle();
        vehicle.setLicensePlate(licensePlate);
        vehicle.setModel(model);
        vehicle.setType(type);
        vehicle.setSeats(seats);
        vehicle.setCanCarpool(canCarpool);
        vehicle.setIsAutonomous(true); // 设置为自动驾驶车辆
        vehicle.setCompanyOwned(true); // 自动驾驶车辆默认是公司车辆
        vehicle.setStatus(VehicleStatus.AVAILABLE);
        vehicle.setLatitude(0); // 初始经纬度
        vehicle.setLongitude(0);

        vehicleRepository.save(vehicle);
        return "自动驾驶车辆添加成功！";
    }

    @PostMapping("/company")
    public String addCompanyVehicle(@RequestBody Map<String, Object> request) {
        String licensePlate = request.get("licensePlate").toString();
        String model = request.get("model").toString();
        String typeStr = request.get("type").toString();
        VehicleType type = VehicleType.valueOf(typeStr);
        int seats = Integer.parseInt(request.get("seats").toString());
        boolean canCarpool = Boolean.parseBoolean(request.get("canCarpool").toString());

        // 检查车牌是否重复
        if (vehicleRepository.existsById(licensePlate)) {
            return "该车牌已被使用！";
        }

        Vehicle vehicle = new Vehicle();
        vehicle.setLicensePlate(licensePlate);
        vehicle.setModel(model);
        vehicle.setType(type);
        vehicle.setSeats(seats);
        vehicle.setCanCarpool(canCarpool);
        vehicle.setIsAutonomous(false); // 设置为非自动驾驶车辆
        vehicle.setCompanyOwned(true); // 设置为公司所有
        vehicle.setStatus(VehicleStatus.unassigned);
        vehicle.setLatitude(0); // 初始经纬度
        vehicle.setLongitude(0);

        vehicleRepository.save(vehicle);
        return "公司车辆添加成功！";
    }
    
    @Operation(summary = "获取所有车辆信息", description = "管理员获取系统中所有车辆的信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功获取所有车辆信息")
    })
    @GetMapping("/all")
    public List<Vehicle> getAllVehicles() {
        return vehicleService.getAllVehicles();
    }
    
    @Operation(summary = "按类型获取车辆信息", description = "获取特定类型的车辆信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功获取特定类型的车辆信息")
    })
    @GetMapping("/by-status")
    public List<Vehicle> getVehiclesByStatus(VehicleStatus status) {
        return vehicleService.findByStatus(status);
    }
    
    @Operation(summary = "获取公司车辆信息", description = "获取所有公司所有的车辆信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功获取公司车辆信息")
    })
    @GetMapping("/company-owned")
    public List<Vehicle> getCompanyOwnedVehicles() {
        return vehicleService.getCompanyOwnedVehicles();
    }
} 